package com.weslide.weslideapp.manager.api;

import org.json.JSONException;

import android.util.Log;

import com.gt.cl.http.CLConnectionException;
import com.gt.cl.http.CLHttpClient;
import com.gt.cl.http.CLInvalidNetworkException;
import com.gt.cl.util.CLFileUtil;
import com.weslide.weslideapp.Constants;
import com.weslide.weslideapp.WeSlideApplication;
import com.weslide.weslideapp.parser.AddScoreParser;
import com.weslide.weslideapp.parser.ChangePwdParser;
import com.weslide.weslideapp.parser.ExchangeScoreParser;
import com.weslide.weslideapp.parser.GetAdverImgsParser;
import com.weslide.weslideapp.parser.GetGiftDetailParser;
import com.weslide.weslideapp.parser.GetGiftsParser;
import com.weslide.weslideapp.parser.GetShopTypeParser;
import com.weslide.weslideapp.parser.GetZonesParser;
import com.weslide.weslideapp.parser.LoginParser;
import com.weslide.weslideapp.parser.RegisterParser;
import com.weslide.weslideapp.pojo.BasePo;
import com.weslide.weslideapp.pojo.ao.AddScoreAO;
import com.weslide.weslideapp.pojo.ao.AdverImgListAO;
import com.weslide.weslideapp.pojo.ao.AdverimgsAO;
import com.weslide.weslideapp.pojo.ao.ExchangeScoreAO;
import com.weslide.weslideapp.pojo.ao.GiftAO;
import com.weslide.weslideapp.pojo.ao.GiftListAO;
import com.weslide.weslideapp.pojo.ao.LoginAO;
import com.weslide.weslideapp.pojo.ao.MyExchangeRecordAO;
import com.weslide.weslideapp.pojo.ao.RegisterAO;
import com.weslide.weslideapp.pojo.ao.ShopTypeAO;
import com.weslide.weslideapp.pojo.ao.ShopTypeList;
import com.weslide.weslideapp.pojo.ao.ZoneListAO;

public class APIManager {
	private static final String TAG = "APIManager";
	private static APIManager mInstance;
	private WeSlideApplication mContext;

	private APIManager() {
		super();
	}

	public void init(WeSlideApplication context) {
		this.mContext = context;
	}

	public static APIManager getInstance() {
		if (mInstance == null) {
			mInstance = new APIManager();
		}
		return mInstance;
	}

	public LoginAO loginAccount(String name, String password)
			throws CLConnectionException, CLInvalidNetworkException,
			JSONException {
		Log.i(TAG, "name >>> " + name);
		Log.i(TAG, "password >>> " + password);
		LoginAO loginAO = new LoginAO();
		String url = Constants.API_LOGIN;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("name", name);
		httpClient.addParam("pwd", password);
		String result = httpClient.postUseingConnection();
		Log.i(TAG, "Result >>> " + result);
		LoginParser parser = new LoginParser();
		parser.parser(result);
		loginAO = parser.getData();
		Log.i(TAG, "name >>> " + loginAO.getName());
		return loginAO;
	}

	public LoginAO register(String name, String password, String invitationCode)
			throws CLConnectionException, CLInvalidNetworkException,
			JSONException {
		LoginAO registerAO = new LoginAO();
		String url = Constants.API_REGISTER;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("name", name);
		httpClient.addParam("pwd", password);
		httpClient.addParam("invitecode", invitationCode);
		String result = httpClient.postUseingConnection();
		RegisterParser parser = new RegisterParser();
		parser.parser(result);
		registerAO = parser.getData();
		Log.i(TAG, "name >>> " + registerAO.getName());
		return registerAO;
	}

	public AdverImgListAO getAdverImgs(String userId)
			throws CLConnectionException, CLInvalidNetworkException,
			JSONException {
		AdverImgListAO adverImgListAO = new AdverImgListAO();
		String url = Constants.API_ADVERIMGS;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("user_id", userId);
		String result = httpClient.getString();
		Log.i(TAG, "Result >>> " + result);
		GetAdverImgsParser parser = new GetAdverImgsParser();
		parser.parser(result);
		adverImgListAO = parser.getData();
		Log.i(TAG, "imgUrl >>> " + adverImgListAO.getStatus());
		return adverImgListAO;
	}

	public GiftListAO getGifts(String userId, String zoneId, String shopTypeId,
			String score) throws CLConnectionException,
			CLInvalidNetworkException, JSONException {
		GiftListAO giftListAO = new GiftListAO();
		String url = Constants.API_GET_GIFTS;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		Log.i(TAG, "userID >>> " + userId);
		Log.i(TAG, "uzone_id >>> " + zoneId);
		Log.i(TAG, "shoptype_id >>> " + shopTypeId);
		Log.i(TAG, "score >>> " + score);
		httpClient.addParam("user_id", userId);
		httpClient.addParam("zone_id", zoneId);
		httpClient.addParam("shoptype_id", shopTypeId);
		httpClient.addParam("score", score);
		String result = httpClient.getString();
		Log.i(TAG, "Result >>> " + result);
		GetGiftsParser parser = new GetGiftsParser();
		parser.parser(result);
		giftListAO = parser.getData();
		return giftListAO;
	}

	public GiftAO getGiftDetail(String goodId) throws CLConnectionException,
			CLInvalidNetworkException, JSONException {
		GiftAO giftAO = new GiftAO();
		String url = Constants.API_GET_GIFTDETAIL;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("good_id", goodId);
		String result = httpClient.getString();
		Log.i(TAG, "Result >>> " + result);
		GetGiftDetailParser parser = new GetGiftDetailParser();
		parser.parser(result);
		giftAO = parser.getData();
		return giftAO;
	}

	public AddScoreAO addScore(String userId) throws CLConnectionException,
			CLInvalidNetworkException, JSONException {
		AddScoreAO addScoreAO = new AddScoreAO();
		String url = Constants.API_ADDSCORE;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("user_id", userId);
		String result = httpClient.getString();
		Log.i(TAG, "Result >>> " + result);
		AddScoreParser parser = new AddScoreParser();
		parser.parser(result);
		addScoreAO = parser.getData();
		return addScoreAO;
	}

	public ZoneListAO getZoneList() throws CLConnectionException,
			CLInvalidNetworkException, JSONException {
		ZoneListAO zoneListAO = new ZoneListAO();
		String url = Constants.API_GETZONES;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		String result = httpClient.getString();
		Log.i(TAG, "Result >>> " + result);
		GetZonesParser parser = new GetZonesParser();
		parser.parser(result);
		zoneListAO = parser.getData();
		return zoneListAO;
	}

	public ShopTypeList getShopTypes() throws CLConnectionException,
			CLInvalidNetworkException, JSONException {
		ShopTypeList shopTypeList = new ShopTypeList();
		String url = Constants.API_GET_SHOPTYPES;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		String result = httpClient.getString();
		Log.i(TAG, "Result >>> " + result);
		GetShopTypeParser parser = new GetShopTypeParser();
		parser.parser(result);
		shopTypeList = parser.getData();
		return shopTypeList;
	}

	public BasePo changePassword(String userId, String oldPassword,
			String newPassword) throws CLConnectionException,
			CLInvalidNetworkException, JSONException {
		BasePo basePo = new BasePo();
		String url = Constants.API_CHANGEPWD;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("user_id", userId);
		httpClient.addParam("oldpwd", oldPassword);
		httpClient.addParam("newpwd", newPassword);
		String result = httpClient.postUseingConnection();
		Log.i(TAG, "Result >>> " + result);
		ChangePwdParser parser = new ChangePwdParser();
		parser.parser(result);
		basePo = parser.getData();
		return basePo;
	}

	public ExchangeScoreAO exchangeScore(String userId, String goodId,
			String phone, String adress, String realname, int count)
			throws CLConnectionException, CLInvalidNetworkException,
			JSONException {
		Log.i(TAG, "phone >>> " + phone);
		Log.i(TAG, "adress >>> " + adress);
		Log.i(TAG, "realname >>> " + realname);
		Log.i(TAG, "count >>> " + count);
		ExchangeScoreAO exchangeScoreAO = new ExchangeScoreAO();
		String url = Constants.API_EXCHANGE_SCORE;
		CLHttpClient httpClient = new CLHttpClient(url, mContext);
		httpClient.addParam("user_id", userId);
		httpClient.addParam("good_id", goodId);
		httpClient.addParam("Realname", realname);
		httpClient.addParam("Phone", phone);
		httpClient.addParam("Address", adress);
		httpClient.addParam("tcount", "" + count);
		String result = httpClient.postUseingConnection();
		Log.i(TAG, "Result >>> " + result);
		ExchangeScoreParser parser = new ExchangeScoreParser();
		parser.parser(result);
		exchangeScoreAO = parser.getData();
		return exchangeScoreAO;
	}

	public MyExchangeRecordAO getMyExchangeRecord() {
		MyExchangeRecordAO exchangeRecordAO = new MyExchangeRecordAO();
		return exchangeRecordAO;
	}
}
